package com.vipheyue.lanjingapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.SensorEvent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import com.umeng.scrshot.UMScrShotController;
import com.umeng.scrshot.adapter.UMAppAdapter;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sensor.UMSensor;
import com.umeng.socialize.sensor.beans.ShakeMsgType;
import com.umeng.socialize.sensor.controller.UMShakeService;
import com.umeng.socialize.sensor.controller.impl.UMShakeServiceFactory;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.vipheyue.lanjingapp.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareActivity extends Activity {

    @Bind(R.id.bt_share)
    Button bt_share;
    UMSocialService mController;
    private UMImage mShareImageview;
    private String   shareTitle = "巨业好工作~~~~~~~";
    private String shareContent="content让移动应用快速整合社交分享功能 -- ";
//设置点击分享内容的跳转链接
    private String  targetUrl = "https://www.baidu.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.bt_share)
    public void onClick() {
//        preShare();
//        detailShare();
        registerShakeToShare();
//    registerShakeToShortCut();
    }

    private void registerShakeToShortCut() {
        UMShakeService mShakeController = UMShakeServiceFactory
                .getShakeService("umeng");
        // 摇一摇截图
        mShakeController.registerShakeToScrShot(this,
                new UMAppAdapter(this), 1500, true,
                mScreenshotListener);
    }

    private void registerShakeToShare() {
        // 声明mShakeController, 参数1为sdk 控制器描述符
        UMShakeService mShakeController = UMShakeServiceFactory
                .getShakeService("com.umeng.share");
        /**
         * 摇一摇截图,直接分享 参数1: 当前所属的Activity 参数2: 截图适配器 参数3: 要用户可选的平台,最多支持五个平台 参数4:
         * 传感器监听器，包括摇一摇完成后的回调函数onActionComplete, 可在此执行类似于暂停游戏、视频等操作;
         * 还有分享完成、取消的回调函数onOauthComplete、onShareCancel。
         */
        preShare();
        UMAppAdapter appAdapter = new UMAppAdapter(this);
        // 配置平台
        List<SHARE_MEDIA> platforms = new ArrayList<SHARE_MEDIA>();
        platforms.add( SHARE_MEDIA.QQ );
        platforms.add( SHARE_MEDIA.QZONE );
        platforms.add( SHARE_MEDIA.WEIXIN );
        platforms.add( SHARE_MEDIA.WEIXIN_CIRCLE );
//        new SmsHandler().addToSocialSDK();
        // 通过摇一摇控制器来设置文本分享内容
        mShakeController.setShareContent(shareTitle);
        mShakeController.setShakeMsgType(ShakeMsgType.PLATFORM_SCRSHOT);

        mShakeController.registerShakeListender(this, appAdapter,
                2000, false, platforms, mSensorListener);
        mShakeController.enableShakeSound(false);

    }
    /**
     * 准备分享之前
     */
    private void preShare() {
        //TODO 替换id,和 密码
        /********分享功能*************************************/
        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "1104708077", "Ejd6EhaBcpNTHNwy");
        qqSsoHandler.addToSocialSDK();

        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, "1104708077", "Ejd6EhaBcpNTHNwy");
        qZoneSsoHandler.addToSocialSDK();

        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        String appID = "wx882e79ce14448faf";//TODO 修改appid,和密码 第二个,第三个
        String appSecret = "941072e9ed09c9a902941c2bb7118623";
// 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(this, appID, appSecret);
        wxHandler.addToSocialSDK();
// 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(this, appID, appSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
        //新浪微博

    }

    private void detailShare() {
        // 首先在您的Activity中添加如下成员变量
        mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        // 设置分享内容

        mController.setShareContent(shareTitle);
        // 设置分享图片, 参数2为图片的url地址
//        mController.setShareMedia(new UMImage(this, "http://www.umeng.com/images/pic/home/social/banner.png"));
        mController.setShareMedia(mShareImageview);
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
        /**QQ---------------------------------------------------*/
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享文字
        qqShareContent.setShareContent(shareContent);
        //设置分享title
        qqShareContent.setTitle(shareTitle);
        //设置分享图片
        qqShareContent.setShareImage(mShareImageview);

        qqShareContent.setTargetUrl(targetUrl);
        mController.setShareMedia(qqShareContent);
        /**QQ--------END-------------------------------------------*/


        /**QZONE--------START-------------------------------------------*/

        QZoneShareContent qzone = new QZoneShareContent();
        //设置分享文字
        qzone.setShareContent(shareContent);
        //设置点击消息的跳转URL
        qzone.setTargetUrl(targetUrl);
        //设置分享内容的标题
        qzone.setTitle(shareTitle);
        //设置分享图片
        qzone.setShareImage(mShareImageview);
        mController.setShareMedia(qzone);
        /**QZONE--------END-------------------------------------------*/
        /**weixin--------start-------------------------------------------*/
        //设置微信好友分享内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
//设置分享文字
        weixinContent.setShareContent(shareContent);
//设置title
        weixinContent.setTitle(shareTitle);
//设置分享内容跳转URL
        weixinContent.setTargetUrl(targetUrl);
//设置分享图片
        weixinContent.setShareImage(mShareImageview);
        mController.setShareMedia(weixinContent);
        /**weixin--------END-------------------------------------------*/
        /**朋友圈--------start-------------------------------------------*/
//设置微信朋友圈分享内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(shareContent);
//设置朋友圈title
        circleMedia.setTitle(shareTitle);
        circleMedia.setShareImage(mShareImageview);
        circleMedia.setTargetUrl(targetUrl);
        mController.setShareMedia(circleMedia);
        /**朋友圈--------END-------------------------------------------*/
        /**新浪--------start-------------------------------------------*/
        //设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        /**新浪--------END-------------------------------------------*/
        /**腾讯微博--------start-------------------------------------------*/
        //设置腾讯微博SSO handler
        mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        /**腾讯微博--------END-------------------------------------------*/


        mController.getConfig().setSsoHandler(new SinaSsoHandler());

        // 是否只有已登录用户才能打开分享选择页
//        mController.openShare(this, false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();

        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    /**
     * 截图监听器，返回屏幕截图
     */
    private UMScrShotController.OnScreenshotListener mScreenshotListener = new UMScrShotController.OnScreenshotListener() {

        @Override
        public void onComplete(Bitmap bmp) {
            if (bmp != null) {
                mShareImageview = new UMImage(ShareActivity.this, bmp);
                preShare();
                detailShare();
            }

        }
    };
    public UMSensor.OnSensorListener mSensorListener = new UMSensor.OnSensorListener() {
        @Override
        public void onStart() {
        }
        /**
         * 分享完成后回调
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int eCode, SocializeEntity entity) {
            Toast.makeText(ShareActivity.this, "分享完成", Toast.LENGTH_SHORT).show();
        }

        /**
         * @Description: 摇一摇动作完成后回调
         */
        @Override
        public void onActionComplete(SensorEvent event) {
//            Toast.makeText(ShareActivity.this, "用户摇一摇，可在这暂停游戏", Toast.LENGTH_SHORT).show();
        }

        /**
         * @param button 用户在分享窗口点击的按钮，有取消和分享两个按钮
         * @Description: 用户点击分享窗口的取消和分享按钮触发的回调
         */
        @Override
        public void onButtonClick(UMSensor.WhitchButton button) {
            if (button == UMSensor.WhitchButton.BUTTON_CANCEL) {
//                Toast.makeText(ShareActivity.this, "取消分享,游戏重新开始", Toast.LENGTH_SHORT).show();
            } else {
                // 分享中, ( 用户点击了分享按钮 )
            }
        }
    };


}
